package vincent.com.layouttest.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

/**
 * Created by zhengwenchen on 2016/4/22.
 */
public class ImageUtil {
    public static Bitmap circleHead(Bitmap source, int width, int height, int borderSize,
                                    int circleColor) {
        Bitmap ret = null;
        if (source != null) {
            Bitmap zoomBitmap = ImageUtil.zoomBitmap(source, width, width);
            Bitmap roundedCornerBitmap = ImageUtil.getRoundedBitmapWithBorder(zoomBitmap, 5.0f,
                    borderSize, circleColor);
            ret = createBitmap(roundedCornerBitmap, 0, 0, width + borderSize * 2,
                    height + borderSize * 2);
            zoomBitmap.recycle();
            roundedCornerBitmap.recycle();
        }
        return ret;
    }

    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) {
        Bitmap bitmap = null;
        if (source != null && !source.isRecycled()) {
            try {
                bitmap = Bitmap.createBitmap(source, x, y, width, height);
            } catch (OutOfMemoryError e) {
                System.gc();
                try {
                    bitmap = Bitmap.createBitmap(source, x, y, width, height);
                } catch (OutOfMemoryError e1) {
                }
            } catch (Exception e2) {
            }
        }
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8);
        }
        return bitmap;
    }

    /**
     * 缩放图片
     *
     * @param bitmap 源图片
     * @param w 新图片宽
     * @param h 新图片高
     * @return 新图片
     */
    public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
        if (bitmap == null) {
            return null;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidht = ((float) w / width);
        float scaleHeight = ((float) h / height);
        matrix.postScale(scaleWidht, scaleHeight);
        Bitmap newbmp = createBitmap(bitmap, 0, 0, width, height, matrix, true);
        // bitmap.recycle();
        return newbmp;
    }

    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m,
                                      boolean filter) {
        Bitmap bitmap = null;
        if (source != null && !source.isRecycled()) {
            try {
                bitmap = Bitmap.createBitmap(source, 0, 0, width, height, m, true);
            } catch (OutOfMemoryError e) {
                System.gc();
                try {
                    bitmap = Bitmap.createBitmap(source, 0, 0, width, height, m, true);
                } catch (OutOfMemoryError e1) {
                }
            } catch (Exception e2) {
            }
        }
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8);
        }
        return bitmap;
    }

    /**
     * 获得带边框圆形的方法
     *
     * @param bitmap 源图片
     * @param roundPx 圆角半径
     * @param boderPx 背景边框宽度
     * @param boderColor 背景边框颜色
     * @return 新图片
     */
    public static Bitmap getRoundedBitmapWithBorder(Bitmap bitmap, float roundPx, int boderPx,
                                                    int boderColor) {
        Bitmap tmpBitmap = getRoundedBitmap(bitmap, roundPx);
        if (tmpBitmap == null) {
            return null;
        }

        Bitmap output = createBitmap(tmpBitmap.getWidth() + boderPx * 2,
                tmpBitmap.getHeight() + boderPx * 2, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint borderpaint = new Paint();
        final Rect borderrect = new Rect(0, 0, tmpBitmap.getWidth() + boderPx * 2,
                tmpBitmap.getHeight() + boderPx * 2);
        final RectF borderrectF = new RectF(borderrect);
        borderpaint.setAntiAlias(true);
        borderpaint.setColor(boderColor);
        // drawOval方法用于绘制一个包含borderrectF的圆
        canvas.drawOval(borderrectF, borderpaint);

        final Paint paint = new Paint();
        final Rect src = new Rect(0, 0, tmpBitmap.getWidth(), tmpBitmap.getHeight());
        final Rect dst = new Rect(boderPx, boderPx, tmpBitmap.getWidth() + boderPx,
                tmpBitmap.getHeight() + boderPx);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        // src参数是图片原来的大小，dst参数是 绘画该图片需显示多大
        canvas.drawBitmap(tmpBitmap, src, dst, paint);
        return output;
    }

    /**
     * 获得圆形的方法
     *
     * @param bitmap 源图片
     * @param roundPx 圆角半径
     * @return 新图片
     */
    public static Bitmap getRoundedBitmap(Bitmap bitmap, float roundPx) {
        if (bitmap == null) {
            return null;
        }
        Bitmap output = createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawOval(rectF, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    public static Bitmap createBitmap(int width, int height, Bitmap.Config config) {
        Bitmap bitmap = null;
        try {
            bitmap = Bitmap.createBitmap(width, height, config);
        } catch (OutOfMemoryError e) {
            System.gc();
            try {
                bitmap = Bitmap.createBitmap(width, height, config);
            } catch (OutOfMemoryError e1) {
            }
        } catch (Exception e2) {
        }
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8);
        }
        return bitmap;
    }
}
